package Warning;

import User.FunctionPkg.OwnDatabase;
import com.eltima.components.ui.DatePicker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;

public class SearchUI extends ShowUI implements ItemListener,ActionListener{
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    JLabel FindTiZhi = new JLabel("体质",SwingConstants.CENTER);
    JLabel Time = new JLabel("时间",SwingConstants.CENTER);
    DatePicker timepicker;
    JButton sure = new JButton("确认");
    OwnDatabase ob;
    //    体质选择下拉表
    JComboBox<String> ChooseTZ;
    String[] type = {"全部体质","平和质","气虚质","阳虚质","阴虚质","痰湿质","湿热质","血瘀质","气郁质","特禀质","兼夹体质"};
    String SearchType = "";
    Font labelfont = new Font("楷体",Font.BOLD,20);
    public SearchUI(String title, String con, OwnDatabase jt){
        super(title,con);
        String format = "yyyy-MM-dd";
        setLayout(null);
        ob=jt;
        ChooseTZ = new JComboBox<>(type);
//        设置时间选择器
        Time.setFont(labelfont);
        Date date = new Date();
        Font font = new Font("Times New Roman", Font.BOLD, 14);
        Dimension dimension = new Dimension(200,30);
        timepicker = new DatePicker(date,format,font,dimension);
        timepicker.setLocale(Locale.US);
//        设置体质选择器
        ChooseTZ.addItemListener(this);
        FindTiZhi.setFont(labelfont);
        sure.setFont(labelfont);
        setSize(400,300);
        int len=60;
        FindTiZhi.setBounds(50,len,60,30);
        ChooseTZ.setBounds(120,len,200,30);
        len+=40;
        Time.setBounds(50,len,60,30);
        timepicker.setLocation(120,len);
        sure.setBounds(150,150,100,60);
        add(sure);
        add(FindTiZhi);
        add(ChooseTZ);
        add(Time);
        add(timepicker);
        sure.addActionListener(this);
        setVisible(false);
    }
    public void itemStateChanged(ItemEvent e){
        Object obj = e.getSource();
        String choose = (String)e.getItem();
        if(obj==ChooseTZ){
            if(e.getStateChange()==ItemEvent.SELECTED){
                SearchType =choose;
            }
        }
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==sure){
            String date = dateFormat.format(timepicker.getValue());
            if(SearchType.equals("全部体质")){
                SearchType = "";
            }
            ob.Search(SearchType,date);
        }
    }
    public void start(){
        setVisible(true);
    }
}
